package com.frontop.system.modules.user.service.impl;

import com.frontop.common.util.ResultJsonUtil;
import com.frontop.dubbo.api.auth.Oauth2Api;
import com.frontop.system.config.NacosConfig;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frontop.common.util.JwtUtil;
import com.frontop.system.modules.user.mapper.TUserMapper;
import com.frontop.system.modules.user.model.TUser;
import com.frontop.system.modules.user.model.TUserShowInfo;
import com.frontop.system.modules.user.service.TUserService;
import com.frontop.system.modules.user.vo.TUserVO;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 用户表	 服务实现类
 * </p>
 *
 * @author YangJia
 * @since 2021-09-13
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Autowired
    TUserMapper tUserMapper;
    @Autowired
    NacosConfig nacosConfig;

    @Reference
    private Oauth2Api oauth2Api;

    @Override
    public Page<TUserShowInfo> selectUserPage(Page<TUserShowInfo> page, TUserVO vo) {
        return tUserMapper.selectUserPage(page,vo);
    }


    @Override
    public TUserShowInfo selectShowUserInfo(String userName) {
        return tUserMapper.selectShowUserInfo(userName);
    }


    @Override
    public String getThisLoginUserName(Authentication authentication) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        String userName = (String) claims.get("user_name");
        return userName;
    }

    @Override
    public String userUploadHeadImage(String userName, MultipartFile multipartFile) {

        //获取文件后缀
        String fileType = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));


        try {
            String uuid = String.valueOf(UUID.randomUUID());
            String uploadPath = nacosConfig.getUploadPath() + "user_head_image/"+userName+"-"+uuid+fileType;

            File file = new File(nacosConfig.getUploadPath() + "user_head_image");
            if(!file.exists()){
                file.mkdirs();
            }
            multipartFile.transferTo(new File(uploadPath));

            //删除旧头像
            String loPath = tUserMapper.selectShowUserInfo(userName).getHeadImage();
            if(StringUtils.isNotEmpty(loPath) && !loPath.equals("defult.gif")){
                File loFile = new File(nacosConfig.getUploadPath() + "user_head_image/"+loPath);
                if (loFile.exists()) {
                    loFile.delete();
                }
            }


            Map<String,Object> map = new HashMap<>();
            map.put("userName",userName);
            map.put("headImage",userName+"-"+ uuid +fileType);
            tUserMapper.updateUserHead(map);
            return userName+"-"+ uuid +fileType;
        } catch (IOException e) {
            e.printStackTrace();
        }
            return "";
    }

    @Override
    public ResultJsonUtil<Object> updatePassword(String oldPwd, String newPwd, String repeatPwd, String userName) {
        //验证重复密码是否相同
        if(!newPwd.equals(repeatPwd)){
            return new ResultJsonUtil().customFail("两次密码不一致");
        }
        //验证旧密码
        String userPassword =tUserMapper.selectPasswordByUserName(userName);
        String passwordEnc = oauth2Api.getEncodePassword(oldPwd);
        if(!passwordEnc.equals(userPassword)){
            return new ResultJsonUtil().customFail("旧密码不正确");
        }
        //获取加密密码
        String newPasswordEnc = oauth2Api.getEncodePassword(newPwd);
        //验证修改的密码是否重复
        if(!newPasswordEnc.equals(userPassword)){
            return new ResultJsonUtil().customFail("不能修改与当前一样的密码");
        }
        //修改密码
        tUserMapper.updatePasswordByUserName(userName,newPasswordEnc);

        return new ResultJsonUtil().success("修改成功");
    }

    @Override
    public int deleteUserByUserName(String userName) {
        return tUserMapper.deleteUserByUserName(userName);
    }


}